﻿
var elId                = $('#userid'),
    elPassword          = $('#password'),
    elNewPassword       = $('#newpassword'),
    elPhone             = $('#phone'),
    elError             = undefined;

elId.on('focusout', function (){
    checkId();
});
elPassword.on('focusout', function (){
    elError = elPassword.next().next();
    checkPassword();
});
elNewPassword.on('focusout', function (){
    elError = elNewPassword.next().next();
    checkNewPassword();
});
elPhone.on('focusout', function (){
    elError = elPhone.next().next();
    checkPhone();
})

function checkId(){
    var id = elId.val().trim();
    if(elId.val() !== id){
        elId.next().next().text('用户名不能包含空格');
        return;
    }
    if(id === null || id === '' || id.length === 0){
        elId.next().next().text('用户名不得为空');
        return;
    }
    elId.next().next().text('');
    $.ajax({
        type    : 'GET',
        url     : 'CheckUserid?type=change&userid=' + elId.val(),
        success : function (data){
            if(data === 'Exist'){
                elId.next().next().text('用户名已存在');
            }
        },
        error   : function (errorMsg){
            console.log(errorMsg);
        }
    });
}

function checkPassword(){
    var password = elPassword.val();
    if(password === null || password === '' || password.length === 0){
        elError.text('');
        return;
    }
    elError.text('');
    $.ajax({
        type    : 'GET',
        url     : 'CheckPassword?password=' + elPassword.val(),
        success : function (data){
            if(data === 'Different'){
                elError.text('原密码输入错误');
            }
        },
        error   : function (errorMsg){
            console.log(errorMsg);
        }
    });
}

function checkNewPassword(){
    var password = elPassword.val();
    var newPassword = elNewPassword.val();
    if(password === null || password === '' || password.length === 0){
        elError.text('');
        if(newPassword.length > 0){
            elError.text('未输入原密码');
        }
        return;
    }
    if(newPassword === null || newPassword === '' || newPassword.length === 0){
        elError.text('新密码不能为空');
        return;
    }
    if(password === newPassword){
        elError.text('新旧密码不能相同');
        return;
    }
    elError.text('');
}

function checkPhone(){
    var phone = elPhone.val().trim();
    var reg = /[^0123456789-]/
    if(phone !== elPhone.val()){
        elError.text('手机号不能包含空格');
        return;
    }
    if(phone === null || phone === '' || phone.length === 0){
        elError.text('手机号不能为空');
        return;
    }
    if(reg.test(phone)){
        elError.text('手机号必须为纯数字');
        return;
    }
    elError.text('');
}